﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Constructor
{
    public partial class FieldItem : UserControl
    {
        private bool _active;
        public Frame Frame { get; set; }
        public bool Active
        {
            get { return _active; }
            set
            {
                _active = value;
                if (_active)
                {
                    this.ForeColor = Color.Red;
                    this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    this.BackColor = Color.Green;
                }
                else
                {
                    this.BorderStyle = System.Windows.Forms.BorderStyle.None;
                    this.BackColor = Color.White;
                }
            }
        }
        public FieldItem(Frame f)
        {
            InitializeComponent();
            Frame = f;
            Init(f);
        }
        public void Move(int row, int col)
        {
            TableLayoutPanel panel = Parent as TableLayoutPanel;
            if (panel != null)
            {
                if (panel.GetControlFromPosition(col, row) != null)
                {
                    Control old = panel.GetControlFromPosition(col, row);
                    TableLayoutPanelCellPosition pos = panel.GetPositionFromControl(this);
                    if (old == this) return;
                    panel.Controls.Remove(old);
                    panel.Controls.Add(this, col, row);
                    panel.Controls.Add(old, pos.Column, pos.Row);
                    External.exemplars.field.SetCell((old as FieldItem).Frame, External.exemplars.field.GetCellByCoord(pos.Row, pos.Column));
                }
                else
                    panel.Controls.Add(this, col, row);
                External.exemplars.field.SetCell(this.Frame, External.exemplars.field.GetCellByCoord(row, col));
            }
        }
        public void Move(Frame cell)
        {
            TableLayoutPanel panel = Parent as TableLayoutPanel;
            panel.SuspendLayout();
            if (panel != null && cell != null)
            {
                Field.Coord coord = External.exemplars.field.GetCoordByCell(cell);
                int col = coord.y;
                int row = coord.x;
                if (panel.GetControlFromPosition(col, row) != null)
                {
                    Control old = panel.GetControlFromPosition(col, row);
                    TableLayoutPanelCellPosition pos = panel.GetPositionFromControl(this);
                    panel.Controls.Remove(old);
                    panel.Controls.Add(this, col, row);
                    panel.Controls.Add(old, pos.Column, pos.Row);
                    External.exemplars.field.SetCell((old as FieldItem).Frame, cell);
                }
                else
                    panel.Controls.Add(this, col, row);
                External.exemplars.field.SetCell(this.Frame, cell);
            }
            
            panel.ResumeLayout();
        }

        public void Init(Frame f)
        {
            if (f != null && f.ContainsImageSlot())
            {
                Slot slot = f.GetImageSlot();
                if (slot.Default!=null && External.images.LoadedImages.ContainsKey(slot.Default.ToString()))
                {
                    icon.BackgroundImage = External.images.LoadedImages[slot.Default.ToString()].BackgroundImage;
                }
                else
                {
                    icon.Visible = false;
                    lbl_name.Text = Frame.Name;
                }
            }
            else
            {
                icon.Visible = false;
                lbl_name.Text = Frame.Name;
            }
        }

        private void icon_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)            
                DoDragDrop(this, DragDropEffects.All);
            OnClick(EventArgs.Empty);             
        }
        public void Delete()
        {
            External.Parents.Remove(Frame.ID);
            MLO.MLOSource.Exemplars.Remove(Frame);
            Parent.Controls.Remove(this);
            External.exemplars.exemplarProperties.Hide();
        }
        private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void FieldItem_KeyDown(object sender, KeyEventArgs e)
        {
            ProcessKey(e.KeyCode);                    
        }
        private void ProcessKey(Keys KeyCode)
        {
            int row = 0;
            int col = 0;
            if (KeyCode == Keys.Up)
            {
                var pos = (Parent as TableLayoutPanel).GetPositionFromControl(this);
                if (pos.Row == 0)
                    return;
                row = pos.Row - 1;
                col = pos.Column;
            }
            if (KeyCode == Keys.Down)
            {
                var pos = (Parent as TableLayoutPanel).GetPositionFromControl(this);
                if (pos.Row == (Parent as TableLayoutPanel).RowCount - 1)
                    return;
                row = pos.Row + 1;
                col = pos.Column;
            }
            if (KeyCode == Keys.Left)
            {
                var pos = (Parent as TableLayoutPanel).GetPositionFromControl(this);
                if (pos.Column == 0)
                    return;
                row = pos.Row;
                col = pos.Column - 1;
            }
            if (KeyCode == Keys.Right)
            {
                var pos = (Parent as TableLayoutPanel).GetPositionFromControl(this);
                if (pos.Column == (Parent as TableLayoutPanel).ColumnCount - 1)
                    return;
                row = pos.Row;
                col = pos.Column + 1;
            }
            Move(row, col); 
        }
        private void icon_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {   
            ProcessKey(e.KeyCode);        
        }

    }
}
